package com.longrise.android.loaddata.newloaddata;

import android.app.Activity;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;

import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.util.PreferenceUtils;

import java.util.HashMap;

/**
 * Created by chengsan on 16/5/20.
 */
public abstract class LoadDataManagerFather
{

    public enum ResultType
    {
        NoNetWorks,   //没有网络
        LoginOut,     //用户被挤掉
        Success,      //成功
        Failure       //失败
    }
    //上下文对象
    public Context context;
    //cookID
    public String cookID;
    //登录数据集合
    public EntityBean loginInfoBean;
    //版本
    public String appVersion;
    //请求方式 1:get 2:POST 默认POST请求
    public int requestType = 2;
    //没有网络时，内部是否提示，默认不提示
    public boolean showNetNotice = false;
    //请求类型Key
    public static final String REQUEST_TYPE_KEY_NAME = "requestTypeKeyName";
    //SP名称
    public static final String SP_NAME = "LoadDataManagerFatherName";
    //存储登录数据的Key名
    public static final String LOGININFO_KEY_NAME = "loginInfoBeanKeyName";
    //IP名称
    public static final String IP_KEY_NAME = "ipKeyName";
    //服务名称
    public static final String SERVICENAME_KEY_NAME = "serviceNameKeyName";
    //参数名称
    public static final String PARAMBEAN_KEY_NAME = "paramBeanKeyName";
    //存储版本的Key名
    public static final String APPVERSION_KEY_NAME = "appVersionKeyName";
    //缓存目录的Key
    public static final String CACHE_DIC_KEY = "cacheDicKeyName";
    //手机设置的时间的Key
    public static final String CONFIG_CACHE_MOBILE_TIMEOUT_KEY_NAME = "mobileTimeOutKeyName";
    //WI-FI设置的时间间隔Key
    public static final String CONFIG_CACHE_WIFI_TIMEOUT_KEY_NAME = "wifiTimeOutKeyName";
    //是否显示提示的Key
    public static final String SHOW_NOTICE_KEY_NAME = "showNetNoticeKeyName";
    //哈西数组，用于存放请求的对象
    public HashMap<String,HttpRunnable> httpRunnableHashMap;
    //请求次数
    public int count = 0;
    //缓存目录
    public String cacheDic = null;
    //请求的时间间隔
    public int mobileTimeOut  = 30000;  //手机网络默认5分钟
    public int wifiTimeOut    = 18000;   //WI-FI网络默认3分钟

    public LoadDataManagerFather()
    {
        httpRunnableHashMap = new HashMap<String, HttpRunnable>();
    }

    public LoadDataManagerFather(Context context)
    {
        this.context = context;
        httpRunnableHashMap = new HashMap<String, HttpRunnable>();

        if(context != null)
        {
            //从SP中获取登录信息
            String loginInfoStr = PreferenceUtils.getPrefString(SP_NAME,Activity.MODE_PRIVATE,context,LOGININFO_KEY_NAME,null);
            if(null != loginInfoStr)
            {
                this.loginInfoBean = (EntityBean) JSONSerializer.getInstance().DeSerialize(loginInfoStr,EntityBean.class);
            }

            //从SP中获取版本信息
            this.appVersion = PreferenceUtils.getPrefString(SP_NAME,Activity.MODE_PRIVATE,context,APPVERSION_KEY_NAME,null);

            //从SP中获取请求方式信息
            this.requestType = PreferenceUtils.getPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,REQUEST_TYPE_KEY_NAME,2);

            this.showNetNotice = PreferenceUtils.getPrefBoolean(SP_NAME,Activity.MODE_PRIVATE,context,SHOW_NOTICE_KEY_NAME,false);

            this.cacheDic = PreferenceUtils.getPrefString(SP_NAME,Activity.MODE_PRIVATE,context,CACHE_DIC_KEY,
                    Environment.getExternalStorageDirectory().getPath() + "/" + context.getPackageName());

            this.mobileTimeOut = PreferenceUtils.getPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,CONFIG_CACHE_MOBILE_TIMEOUT_KEY_NAME,30000);

            this.wifiTimeOut = PreferenceUtils.getPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,CONFIG_CACHE_WIFI_TIMEOUT_KEY_NAME,18000);
        }
    }




    //==========================GET()/SET()方法=============================
    public void setAppVersion(String appVersion)
    {
        this.appVersion = appVersion;
        if(null != context)
        {
            PreferenceUtils.setPrefString(SP_NAME,
                    Activity.MODE_PRIVATE,context,APPVERSION_KEY_NAME,appVersion);
        }
    }
    public String getAppVersion()
    {
        return appVersion;
    }

    public void setLoginInfoBean(EntityBean loginInfoBean)
    {
        this.loginInfoBean = loginInfoBean;
        if(null != context)
        {
            String loginInfoStr = JSONSerializer.getInstance().Serialize(loginInfoBean);
            PreferenceUtils.setPrefString(SP_NAME,Activity.MODE_PRIVATE,context,LOGININFO_KEY_NAME,loginInfoStr);
        }
    }
    public EntityBean getLoginInfoBean()
    {
        return loginInfoBean;
    }

    public void setRequestType(int requestType)
    {
        this.requestType = requestType;
        if(context != null)
        {
            PreferenceUtils.setPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,REQUEST_TYPE_KEY_NAME,requestType);
        }
    }

    public int getRequestType()
    {
        return requestType;
    }

    public void setShowNetNotice(boolean showNetNotice)
    {
        this.showNetNotice = showNetNotice;
        if(context != null)
        {
            PreferenceUtils.setPrefBoolean(SP_NAME,Activity.MODE_PRIVATE,context,SHOW_NOTICE_KEY_NAME,showNetNotice);
        }
    }

    public void setCacheDic(String cacheDic)
    {
        this.cacheDic = cacheDic;
        if(context != null)
        {
            PreferenceUtils.setPrefString(SP_NAME,Activity.MODE_PRIVATE,context,CACHE_DIC_KEY,cacheDic);
        }
    }

    public String getCacheDic()
    {
        return cacheDic;
    }

    public void setMobileTimeOut(int mobileTimeOut)
    {
        this.mobileTimeOut = mobileTimeOut;
        if(context != null)
        {
            PreferenceUtils.setPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,CONFIG_CACHE_MOBILE_TIMEOUT_KEY_NAME,mobileTimeOut);
        }
    }

    public int getMobileTimeOut()
    {
        return mobileTimeOut;
    }

    public void setWifiTimeOut(int wifiTimeOut)
    {
        this.wifiTimeOut = wifiTimeOut;
        if(context != null)
        {
            PreferenceUtils.setPrefInt(SP_NAME,Activity.MODE_PRIVATE,context,CONFIG_CACHE_WIFI_TIMEOUT_KEY_NAME,wifiTimeOut);
        }
    }

    public int getWifiTimeOut()
    {
        return wifiTimeOut;
    }

    /**
     * 请求完成接口
     */
    public interface OnRequestCompleteListener
    {
        /**
         * 成功接口
         * @param key
         * @param service
         * @param result
         */
        public void onSuccess(String key, String service, Object result);

        /**
         * 出错接口
         * @param key
         * @param service
         * @param resultType
         */
        public void onError(String key, String service,ResultType resultType);

        /**
         * 完成接口，不论是成功或者失败都会调用
         * @param key
         * @param service
         */
        public void onFinished(String key, String service);
    }
}
